<?php

class LoginController extends CController
{
	private $_baseAdminUrl = '/admin/hotel/';
	private $_baseLoginUrl = '/admin/login/';
	
	public function actionIndex()
	{
		$this->layout = 'login';
		
		$this->render('index', array(
			'flashMessage' => FlashMessage::getInfo()
		));
	}
	
	public function actionAuthenticate()
	{
		$username = $_POST['login'];
		$password = $_POST['password'];
		$code     = $_POST['code'];
		
		$identity = new UserIdentity($username, $password, $code);
		
		if ($identity->authenticate()) {
    		Yii::app()->user->login($identity, 3600);
    		$this->redirect($this->_baseAdminUrl);
		} else {
    		FlashMessage::saveInfo(array('error'  => 1));
			$this->redirect($this->_baseLoginUrl);
		}
	}
	
	public function actionCaptcha()
	{
		UserIdentity::showCaptchaSecurityImages();
		die();
	}
	
	public function filterUserCheck($filterChain)
	{
		$auth = UserIdentity::checkAuth();
		
		if ($auth) {
			Yii::app()->request->redirect($this->_baseAdminUrl);
		}
		
		$filterChain->run();
	}
	
	public function filters()
    {
        return array(
            'userCheck + index, authenticate, captcha'
        );
    }
	
}